<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html><head>
<title>An In-Depth Discussion of Virtual Host Matching</title>
</head>

<!-- Background white, links blue (unvisited), navy (visited), red (active) -->
<BODY
 BGCOLOR="#FFFFFF"
 TEXT="#000000"
 LINK="#0000FF"
 VLINK="#000080"
 ALINK="#FF0000"
>
<!--#include virtual="header.html" -->
<h1 ALIGN="CENTER">An In-Depth Discussion of Virtual Host Matching</h1>

<p>The virtual host code was completely rewritten in <B>Apache 1.3</B>.
This document attempts to explain exactly what Apache does when
deciding what virtual host to serve a hit from. With the help of the
new <A HREF="../mod/core.html#namevirtualhost"><SAMP>NameVirtualHost</SAMP></A>
directive  virtual host configuration should be a lot easier and safer
than with versions prior to 1.3.

<p>If you just want to <cite>make it work</cite> without understanding
how, here are <a href="examples.html">some examples</a>.

<h3>Config File Parsing</h3>

<p>There is a <EM>main_server</EM> which consists of all
the definitions appearing outside of <CODE>&lt;VirtualHost&gt;</CODE> sections.
There are virtual servers, called <EM>vhosts</EM>, which are defined by
<A HREF="../mod/core.html#virtualhost"><SAMP>&lt;VirtualHost&gt;</SAMP></A>
sections.

<p>The directives
<A HREF="../mod/core.html#port"><SAMP>Port</SAMP></A>,
<A HREF="../mod/core.html#servername"><SAMP>ServerName</SAMP></A>,
<A HREF="../mod/core.html#serverpath"><SAMP>ServerPath</SAMP></A>,
and
<A HREF="../mod/core.html#serveralias"><SAMP>ServerAlias</SAMP></A>
can appear anywhere within the definition of
a server.  However, each appearance overrides the previous appearance
(within that server).

<p>The default value of the <code>Port</code> field for main_server
is 80.  The main_server has no default <code>ServerPath</code>, or
<code>ServerAlias</code>. The default <code>ServerName</code> is
deduced from the servers IP address.

<p>The main_server Port directive has two functions due to legacy
compatibility with NCSA configuration files.  One function is
to determine the default network port Apache will bind to.  This
default is overridden by the existence of any
<A HREF="../mod/core.html#listen"><code>Listen</code></A> directives.
The second function is to specify the port number which is used
in absolute URIs during redirects.

<p>Unlike the main_server, vhost ports <em>do not</em> affect what
ports Apache listens for connections on.

<p>Each address appearing in the <code>VirtualHost</code> directive
can have an optional port.  If the port is unspecified it defaults to
the value of the main_server's most recent <code>Port</code> statement.
The special port <SAMP>*</SAMP> indicates a wildcard that matches any port.
Collectively the entire set of addresses (including multiple
<SAMP>A</SAMP> record
results from DNS lookups) are called the vhost's <EM>address set</EM>.

<P>Unless a <A HREF="../mod/core.html#namevirtualhost">NameVirtualHost</A>
directive is used for a specific IP address the first vhost with
that address is treated as an IP-based vhost.

<P>If name-based vhosts should be used a <code>NameVirtualHost</code>
directive <em>must</em> appear with the IP address set to be used for the
name-based vhosts. In other words, you must specify the IP address that
holds the hostname aliases (CNAMEs) for your name-based vhosts via a
<code>NameVirtualHost</code> directive in your configuration file.

<P>Multiple <code>NameVirtualHost</code> directives can be used each
with a set of <code>VirtualHost</code> directives.

<P>The ordering of <code>NameVirtualHost</code> and 
<code>VirtualHost</code> directives is not important which makes the
following two examples identical (only the order of the
<code>VirtualHost</code> directives for <em>one</em> address set
is important, see below):

<pre>
                                |
  NameVirtualHost 111.22.33.44  | &lt;VirtualHost 111.22.33.44&gt;
  &lt;VirtualHost 111.22.33.44&gt;    | # server A
  # server A  		        | &lt;/VirtualHost&gt;
  ... 			        | &lt;VirtualHost 111.22.33.55&gt;
  &lt;/VirtualHost&gt;	        | # server C
  &lt;VirtualHost 111.22.33.44&gt;    | ...
  # server B  		        | &lt;/VirtualHost&gt;
  ... 			        | &lt;VirtualHost 111.22.33.44&gt;
  &lt;/VirtualHost&gt;	        | # server B
                                | ...
  NameVirtualHost 111.22.33.55  | &lt;/VirtualHost&gt;
  &lt;VirtualHost 111.22.33.55&gt;    | &lt;VirtualHost 111.22.33.55&gt;
  # server C  		        | # server D
  ... 			        | ...
  &lt;/VirtualHost&gt;	        | &lt;/VirtualHost&gt;
  &lt;VirtualHost 111.22.33.55&gt;    |
  # server D  		        | NameVirtualHost 111.22.33.44
  ... 			        | NameVirtualHost 111.22.33.55
  &lt;/VirtualHost&gt;	        |
                                |
</pre>

<p>(To aid the readability of your configuration you should prefer the
left variant.)

<p> After parsing the <code>VirtualHost</code> directive, the vhost server
is given a default <code>Port</code> equal to the port assigned to the
first name in its <code>VirtualHost</code> directive.

<p>The complete list of names in the <code>VirtualHost</code> directive
are treated just like a <code>ServerAlias</code> (but are not overridden by any
<code>ServerAlias</code> statement) if all names resolve to the same address set.
Note that subsequent <code>Port</code> statements for this vhost will not affect
the ports assigned in the address set.

<p>During initialization a list for each IP address
is generated an inserted into an hash table. If the IP address is
used in a <code>NameVirtualHost</code> directive the list contains
all name-based vhosts for the given IP address. If there are no
vhosts defined for that address the <code>NameVirtualHost</code> directive
is ignored and an error is logged. For an IP-based vhost the list in the
hash table is empty.

<p>Due to a fast hashing function the overhead of hashing an IP address
during a request is minimal and almost not existent. Additionally
the table is optimized for IP addresses which vary in the last octet.

<p>For every vhost various default values are set. In particular:

<ol>
<li>If a vhost has no
    <A HREF="../mod/core.html#serveradmin"><code>ServerAdmin</code></A>,
    <A HREF="../mod/core.html#resourceconfig"><code>ResourceConfig</code></A>,
    <A HREF="../mod/core.html#accessconfig"><code>AccessConfig</code></A>,
    <A HREF="../mod/core.html#timeout"><code>Timeout</code></A>,
    <A HREF="../mod/core.html#keepalivetimeout"><code>KeepAliveTimeout</code></A>,
    <A HREF="../mod/core.html#keepalive"><code>KeepAlive</code></A>,
    <A HREF="../mod/core.html#maxkeepaliverequests"><code>MaxKeepAliveRequests</code></A>,
    or
    <A HREF="../mod/core.html#sendbuffersize"><code>SendBufferSize</code></A>
    directive then the respective value is
    inherited from the main_server.  (That is, inherited from whatever
    the final setting of that value is in the main_server.)

<li>The &quot;lookup defaults&quot; that define the default directory
    permissions
    for a vhost are merged with those of the main_server.  This includes
    any per-directory configuration information for any module.

<li>The per-server configs for each module from the main_server are
    merged into the vhost server.
</ol>

Essentially, the main_server is treated as &quot;defaults&quot; or a
&quot;base&quot; on which to build each vhost.
But the positioning of these main_server
definitions in the config file is largely irrelevant -- the entire
config of the main_server has been parsed when this final merging occurs.
So even if a main_server definition appears after a vhost definition
it might affect the vhost definition.

<p> If the main_server has no <code>ServerName</code> at this point,
then the hostname of the machine that httpd is running on is used
instead.  We will call the <EM>main_server address set</EM> those IP
addresses returned by a DNS lookup on the <code>ServerName</code> of
the main_server.

<p> For any undefined <code>ServerName</code> fields, a name-based vhost
defaults to the address given first in the <code>VirtualHost</code>
statement defining the vhost.

<P>Any vhost that includes the magic <SAMP>_default_</SAMP> wildcard
is given the same <code>ServerName</code> as the main_server.


<h3>Virtual Host Matching</h3>

<p>The server determines which vhost to use for a request as follows:

<h4>Hash table lookup</h4>

<p>When the connection is first made by a client, the IP address to
which the client connected is looked up in the internal IP hash table.

<P>If the lookup fails (the IP address wasn't found) the request is
served from the <samp>_default_</samp> vhost if there is such a vhost
for the port to which the client sent the request. If there is no
matching <samp>_default_</samp> vhost the request is served from the
main_server.

<P>If the lookup succeeded (a corresponding list for the IP address was
found) the next step is to decide if we have to deal with an IP-based
or a name-base vhost.

<h4>IP-based vhost</h4>

<P>If the entry we found has an empty name list then we have found an
IP-based vhost, no further actions are performed and the request is
served from that vhost.

<h4>Name-based vhost</h4>

<p>If the entry corresponds to a name-based vhost the name list contains
one or more vhost structures. This list contains the vhosts in the same
order as the <code>VirtualHost</code> directives appear in the config
file.

<p>The first vhost on this list (the first vhost that appears after the
corresponding <code>NameVirtualHost</code> directive in the config file)
has the highest priority and catches any request to an unknown 
server name or a request without a <code>Host:</code> header.

<p>If the client provided a <code>Host:</code> header the list is
searched for a matching vhost and the first hit on a <code>ServerName</code>
or <code>ServerAlias</code> is taken and the request is served from
that vhost. A <code>Host:</code> header can contain a port number, but
Apache always matches against the real port to which the client sent
the request.

<p>If the client submitted a HTTP/1.0 request without <code>Host:</code>
header we don't know to what server the client tried to connect and
any existing <code>ServerPath</code> is matched against the URI
from the request. The first matching path on the list is used and the
request is served from that vhost.

<p>If no matching vhost could be found the request is served from the
first vhost with a matching port number that is on the list for the IP
to which the client connected (as already mentioned before).

<h4>Persistent connections</h4>
The IP lookup described above is only done <em>once</em> for a particular
TCP/IP session while the name lookup is done on <em>every</em> request
during a KeepAlive/persistent connection. In other words a client may
request pages from different name-based vhosts during a single
persistent connection.


<h4>Absolute URI</h4>

<p>If the URI from the request is an absolute URI, and its hostname and
port match the main server or one of the configured virtual hosts
<em>and</em> match the address and port to which the client sent the request,
then the scheme/hostname/port prefix is stripped off and the remaining
relative URI is served by the corresponding main server or virtual host.
If it does not match, then the URI remains untouched and the request is
taken to be a proxy request.


<h3>Observations</h3>

<ul>

<li>A name-based vhost can never interfere with an IP-base vhost and
    vice versa. IP-based vhosts can only be reached through an IP address
    of its own address set and never through any other address.
    The same applies to name-based vhosts, they can only be reached
    through an IP address of the corresponding address set which must
    be defined with a <code>NameVirtualHost</code> directive.
    <p>

<li><code>ServerAlias</code> and <code>ServerPath</code> checks are never
    performed for an IP-based vhost.
    <p>
    
<li>The order of name-/IP-based, the <samp>_default_</samp>
    vhost and the <code>NameVirtualHost</code> directive within the config
    file is not important. Only the ordering
    of name-based vhosts for a specific address set is significant. The one
    name-based vhosts that comes first in the configuration file has
    the highest priority for its corresponding address set.
    <p>

<li>For security reasons the port number given in a <code>Host:</code>
    header is never used during the matching process. Apache always
    uses the real port to which the client sent the request.
    <p>

<li>If a <code>ServerPath</code> directive exists which is a prefix of
    another <code>ServerPath</code> directive that appears later in
    the configuration file, then the former will always be matched
    and the latter will never be matched.  (That is assuming that no
    Host header was available to disambiguate the two.)
    <p>

<li>If two IP-based vhosts have an address in common, the vhost appearing
    first in the config file is always matched.  Such a thing might happen
    inadvertently. The server will give a warning in the error
    logfile when it detects this.
    <p>
    
<li>A <code>_default_</code> vhost catches a request only if there is no
    other vhost with a matching IP address <em>and</em> a matching port
    number for the request. The request is only catched if the port number
    to which the client sent the request matches the port number of your
    <code>_default_</code> vhost which is your standard <code>Port</code>
    by default. A wildcard port can be specified (i.e.
    <code>_default_:*</code>) to catch requests to any available port.
    <p>
    
<li>The main_server is only used to serve a request if the IP address
    and port number to which the client connected is unspecified
    and does not match any other vhost (including a <code>_default_</code>
    vhost). In other words the main_server only catches a request for an
    unspecified address/port combination (unless there is a <code>_default_</code>
    vhost which matches that port).
    <p>
    
<li>A <code>_default_</code> vhost or the main_server is <em>never</em>
    matched for a request with an unknown or missing <code>Host:</code> header
    if the client connected to an address (and port) which is used
    for name-based vhosts, e.g. in a <code>NameVirtualHost</code> directive.
    <p>
    
<li>You should never specify DNS names in <code>VirtualHost</code>
    directives because it will force your server to rely on DNS to boot.
    Furthermore it poses a security threat if you do not control the
    DNS for all the domains listed.
    There's <a href="../dns-caveats.html">more information</a>
    available on this and the next two topics.
    <p>

<li><code>ServerName</code> should always be set for each vhost.  Otherwise
    A DNS lookup is required for each vhost.
    <p>

</ul>

<h3>Tips</h3>

<p>In addition to the tips on the <a href="../dns-caveats.html#tips">DNS
Issues</a> page, here are some further tips:

<ul>

<li>Place all main_server definitions before any <code>VirtualHost</code>
    definitions. (This is to aid the readability of the configuration --
    the post-config merging process makes it non-obvious that definitions 
    mixed in around virtual hosts might affect all virtual hosts.)
    <p>

<li>Group corresponding <code>NameVirtualHost</code> and
    <code>VirtualHost</code> definitions in your configuration to ensure
    better readability.
    <p>

<li>Avoid <code>ServerPaths</code> which are prefixes of other
    <code>ServerPaths</code>.  If you cannot avoid this then you have to
    ensure that the longer (more specific) prefix vhost appears earlier in
    the configuration file than the shorter (less specific) prefix
    (<EM>i.e.</EM>, &quot;ServerPath /abc&quot; should appear after
    &quot;ServerPath /abc/def&quot;).
    <p>

</ul>

<!--#include virtual="footer.html" -->
</BODY>
</HTML>
